﻿using System;
using System.Windows.Forms;
using System.Text;
using System.Drawing;
using System.Windows.Forms.VisualStyles;

namespace QATools.Components
{
    class ComboBoxCheck : ComboBox
    {
        private string _selection;

        public ComboBoxCheck() {
            this.DrawMode = DrawMode.OwnerDrawVariable;
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            this.FormattingEnabled = true;
        }

        public class ComboBoxCheckItem
        {
            public ComboBoxCheckItem(string text, bool isChecked) {
                _checked = isChecked;
                _text = text;
            }

            private bool _checked = false;
            public bool Checked {
                get { return _checked; }
                set { _checked = value; }
            }

            private string _text = "";
            public string Text {
                get { return _text; }
                set { _text = value; }
            }

            public override string ToString() {
                return _text;
            }
        }

        public void AddItem(string name, bool isChecked) {
            this.Items.Add(new ComboBoxCheck.ComboBoxCheckItem(name, isChecked));
        }

        protected override void OnSelectedValueChanged(EventArgs e) {
            ComboBoxCheckItem selitem = (ComboBoxCheckItem)SelectedItem;
            selitem.Checked = !selitem.Checked;
            this.DroppedDown = true;
            StringBuilder sb = new StringBuilder();
            foreach (ComboBoxCheckItem item in this.Items) {
                if (item.Checked) {
                    if (sb.Length != 0) sb.Append(',');
                    sb.Append(item.Text);
                }
            }
            this._selection = sb.ToString();
        }

        protected override void OnDrawItem(DrawItemEventArgs e) {
            if (e.Index < 0) return;
            e.DrawBackground();

            if (this.DroppedDown == false || e.State == DrawItemState.ComboBoxEdit) {
                e.Graphics.DrawString(
                    _selection,
                    this.Font,
                    Brushes.Black,
                    new Point(e.Bounds.X, e.Bounds.Y));
                return;
            }

            ComboBoxCheckItem cbitem = (ComboBoxCheckItem)Items[e.Index];
            CheckBoxRenderer.RenderMatchingApplicationState = true;
            Rectangle rect = e.Bounds;
            rect.Height += 1;
            CheckBoxRenderer.DrawCheckBox(
                e.Graphics,
                new Point(e.Bounds.X + 1, e.Bounds.Y + 1),
                rect,
                "     " + cbitem.Text,
                this.Font,
                TextFormatFlags.Left,
                false,
                cbitem.Checked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal
            );

            int newWidth = (int)e.Graphics.MeasureString(cbitem.Text, this.Font).Width + 10;
            if (this.DropDownWidth < newWidth)
                this.DropDownWidth = newWidth;

            if ((e.State & DrawItemState.NoFocusRect) == DrawItemState.None)
                e.DrawFocusRectangle();

        }
    }

}



